/**
 * @author YinHang
 */
;(function () {
	window.Enemy = function (image, sWidth, sHeight, speed, world, node, bornTime, hp, cast) {
		window.Enemy.base.call(this, image, sWidth, sHeight);
		this.path = [];
		this.lastX = -1;
		this.lastY = -1;
		this.maxSpeed = this.speed = speed || 1;
		this.id = -1;
		this.world = world;
		this.halfWidth = this.width / 2;
		this.halfHeight = this.height / 2;
		this.enemyGroupNode = node;
		this.hpHeight = 4;
		this.hpWidth = 18;
		this.hpXOffset = this.width / 2 - this.hpWidth / 2;
		this.hpYOffset = -10;
		this.maxHp = 100;
		this.hp = this.maxHp;
		this.bornTime = bornTime || 0;
		this.frameJump = 10;
		this.cast = cast || 20;
	};
	
	Gamepackage.extend(window.Enemy, Gamepackage.ui.g2d.Sprite);
	var Prototype = window.Enemy.prototype;
	
	Prototype.drawSuper = Prototype.draw;
	
	Prototype.getHpColor = function () {
		var halfMaxHp = this.maxHp / 2;
		var colorRatio = this.hp / this.maxHp;
		var greenValue = parseInt( colorRatio >= 0.5 ? 255 : 255 * colorRatio );
		var redValue = parseInt( colorRatio <= 0.5 ? 255 : 255 - 255 * colorRatio );
		return "rgb(" + redValue + ", " + greenValue + ", 0)";
	};
	
	Prototype.canBorn = function () {
		return new Date().getTime() >= this.bornTime;
	};
	
	Prototype.setBornTime = function (bornTime) {
		this.bornTime = bornTime;
	};
	
	Prototype.setHp = function (hp) {
		this.hp = hp;
		if(this.hp < 0)
			this.hp  = 0;
		else if(this.hp > this.maxHp)
		{
			this.hp = this.maxHp;
		}
	};
	
	Prototype.setMaxHp = function (maxHp) {
		this.maxHp = maxHp;
	};
	
	Prototype.getHp = function () {
		return this.hp;
	};
	
	Prototype.dead = function () {
		this.done();
	};
	
	Prototype.hurt = function (hurt) {
		this.setHp(this.hp - hurt);
	};
	
	Prototype.reinstate = function (value) {
		this.setHp(this.hp + value);
	};
	
	Prototype.draw = function (g) {
		this.drawSuper(g);
		var hpX = this.x + this.hpXOffset;
		var hpY = this.y + this.hpYOffset;
		g.drawRect("#000000", hpX, hpY, this.hpWidth, this.hpHeight);
		g.fillRect(this.getHpColor(), hpX + 1, hpY + 1, ( this.hpWidth - 2 ) * ( this.hp / this.maxHp ), this.hpHeight - 2);
	};
	
	Prototype.setPath = function (path) {
		this.path = path;
	};
	
	Prototype.setEnemyGroupNode = function (node) {
		this.enemyGroupNode = node;
	};
	
	Prototype.slowDown = function (dValue) {
		this.speed = this.maxSpeed - dValue;
	};
	
	Prototype.getEnemyGroupNode = function () {
		return this.enemyGroupNode;
	};
	
	Prototype.cleanPath = function () {
		this.path = [];
	};
	
	Prototype.getPath = function () {
		return this.path;
	};
	
	Prototype.setSpeed = function (speed) {
		this.speed = speed;
	};
	
	Prototype.setId = function (id) {
		this.id = id;
	};
	
	Prototype.done = function () {
		this.world.destroyAnEnemy(this);
	};
	
	Prototype.setWorld = function (world) {
		this.world = world;
	};
	
	var num = 0;
	
	Prototype.go = function (offsetX, offsetY) {
		this.loopFrame();
		
		if(this.lastX == -1 || this.lastY == -1 || (this.lastY == this.y && this.lastX == this.x))
		{
			if(this.path.length <= 0)
			{
				this.done();
				-- this.world.playerHp;
			}
			else
			{
				this.lastX = this.path.pop() + offsetX - this.width / 2;
				this.lastY = this.path.pop() + offsetY - this.height / 2;
			}
		}
		if(this.lastX && this.lastY)
			this.moveTo(this.lastX, this.lastY, this.speed);
	};
	
})();
